#include <iostream>
#include <vector>
#include <fstream>
#include <cmath>
#include "Spline.hpp"
#include "Function.hpp"

class F : public Function {
public:
    double operator() (double x) const override {
        return 1 / (1 + x * x); // 示例函数
    }
};

int main() {
    F func;
    std::vector<double> Knots;
    for (int i = 0; i <= 11; ++i) {
        Knots.push_back(-6 + i);
    }

    // 使用函数生成两种B样条对象
    CompleteCubicBSpline Comp_B(Knots, func);
    LinearBSpline Lin_B(Knots, func);
    // 输出插值结果到文件
    std::ofstream outfileC("output/C.txt");
    for (double x = -5; x <= 5; x += 0.01) {
        double y = Lin_B.evaluate(x);
        outfileC<< x << " " << y << std::endl;
    }
    outfileC << std::endl;
    for (double x = -5; x <= 5; x += 0.01) {
        double y = Comp_B.evaluate(x);
        outfileC<< x << " " << y << std::endl;
    }
    outfileC.close();

    // 输出几个特定点的插值结果之差
    std::vector<double> sites={-3.5, -3, -0.5, 0, 0.5, 3, 3.5};

    std::ofstream outfileD("output/D.txt");
    for (double x : sites) {
        outfileD << x << " " << std::abs(Lin_B.evaluate(x)- 1/(1 + x * x)) << std::endl;
    }
    outfileD << std::endl;
    for (double x : sites) {
        outfileD << x << " " << std::abs(Comp_B.evaluate(x)- 1/(1 + x * x))<< std::endl;
    }
    outfileD.close();

    return 0;
}